package com.sky.controller.admin;

import com.sky.dto.OrdersCancelDTO;
import com.sky.dto.OrdersDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.dto.OrdersRejectionDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.OrderService;
import com.sky.vo.OrderStatisticsVO;
import com.sky.vo.OrderVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController("adminordercontroller")
@Slf4j
@RequestMapping("/admin/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    /**
     * 查看订单分页列表
     *
     * @param pageQueryDTO
     * @return
     */
    @GetMapping("/conditionSearch")
    public Result<PageResult> pageList(OrdersPageQueryDTO pageQueryDTO) {
        log.info("接收到前端传过来的用户端分页查询订单的数据为:{}", pageQueryDTO);
        PageResult pageResult = orderService.adminPageQuery(pageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 查询各个状态订单的数量
     *
     * @return
     */
    @GetMapping("/statistics")
    public Result<OrderStatisticsVO> getStatis() {
        OrderStatisticsVO statisticsVO = orderService.getStatis();
        return Result.success(statisticsVO);
    }

    /**
     * 通过id查询订单详情
     *
     * @return
     */
    @GetMapping("details/{id}")
    public Result<OrderVO> getById(@PathVariable Long id) {
        log.info("前端传过来的查找订单详情的id为:{}",id);
        OrderVO orderVO = orderService.getById(id);
        return Result.success(orderVO);
    }

    /**
     * 商家接单
     * @return
     */
    @PutMapping("/confirm")
    public Result acceptOrder(@RequestBody OrdersDTO dto){
        log.info("商家接单的id为:{}",dto);
        Long id = dto.getId();
        orderService.acceptOrder(id);
        return Result.success();
    }

    /**
     * 商家拒单
     * @param dto
     * @return
     */
    @PutMapping("/rejection")
    public Result rejectOrder(@RequestBody OrdersRejectionDTO dto){
        log.info("接收到前端拒单的数据为:{}",dto);
        orderService.rejectOrder(dto);
          return Result.success();
    }

    /**
     * 商家取消订单
     * @param dto
     * @return
     */
    @PutMapping("/cancel")
    public Result cancelOrder(@RequestBody OrdersCancelDTO dto){
        log.info("商家取消订单传过来的数据为:{}",dto);
        orderService.AdminCancelOrder(dto);
        return Result.success();
    }

    /**
     * 派送订单
     * @param id
     * @return
     */
    @PutMapping("/delivery/{id}")
    public Result deliverOrder(@PathVariable Long id){
        log.info("派送订单接收到前端的数据为:{}",id);
        orderService.deliverOrder(id);
        return Result.success();
    }

    /**
     * 商家完成订单
     * @param id
     * @return
     */
    @PutMapping("/complete/{id}")
    public Result completeOrder(@PathVariable Long id){
        log.info("接收到前端传过来的完成订单的数据为:{}",id);
        orderService.completeOrder(id);
        return Result.success();
    }
}
